package com.shouer.doc.application;

import android.app.Application;

import com.alibaba.sdk.android.man.MANService;
import com.alibaba.sdk.android.man.MANServiceProvider;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.karumi.dexter.Dexter;
import com.shouer.doc.ShouerAppContext;
import com.shouer.doc.application.presenter.AppPresenterImpl;
import com.shouer.doc.application.presenter.IAppPresenter;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.SPUtils;
import com.shouer.doc.dao.DaoMaster;
import com.shouer.doc.dao.DaoSession;
import com.shouer.doc.ui.login.model.DoctorInfoModel;
import com.shouer.doc.ui.login.model.UserModel;
import com.tencent.bugly.crashreport.CrashReport;

import org.greenrobot.greendao.database.Database;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;
import io.rong.imlib.model.UserInfo;

/**
 * Created by ZXL on 2016/11/17.
 */

public class App extends Application {

    private UserModel userModel;
    private DoctorInfoModel userDocInfo;
    private IAppPresenter mPresenter;
    private  MANService manService;
    private DaoSession daoSession;

    public void onCreate()
    {
        super.onCreate();
        Dexter.initialize(this);
        RongIM.init(this);
        ShouerAppContext.init(this);
        Fresco.initialize(this);

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"shouerdoc-db");
        Database db = helper.getWritableDb();
        daoSession=new DaoMaster(db).newSession();
        ShouerConst.APP_VERSION = AppUtil.getVersion(this);
        mPresenter=new AppPresenterImpl(this);

        // 获取MAN服务
        manService = MANServiceProvider.getService();
        manService.getMANAnalytics().setChannel(AppUtil.getApplicationMetaData(this,"CHANNEL"));
        manService.getMANAnalytics().init(this, getApplicationContext());
        manService.getMANAnalytics().setAppVersion(AppUtil.getVersion(this));

        CrashReport.initCrashReport(getApplicationContext(), "cbfd35065a", false);
    }

    public UserModel getUserModel() {
        return userModel;
    }

    public void setUserModel(UserModel userModel) {
        this.userModel = userModel;
    }

    public DoctorInfoModel getUserDocInfo() {
        return userDocInfo;
    }

    public String getUserDocId() {

        if (null!=userDocInfo)
        {
            return userDocInfo.getDocId();
        }
        return null;
    }

    public void setUserDocInfo(DoctorInfoModel userDocInfo) {
        this.userDocInfo = userDocInfo;
    }

    public String getRongToken()
    {
        if (null!=userModel)
            return userModel.getRongToken();
        return null;
    }

    public boolean isUserLogin()
    {
        if (null==userModel)
        {
            return false;
        }else
        {
            return true;
        }
    }

    public void loginOut()
    {
        if (null!=manService)
        {
            manService.getMANAnalytics().updateUserAccount("", "");
        }
        mPresenter.logout(userModel.getUserId());
        SPUtils.put(this,"user_pass","");
        RongIM.getInstance().logout();
        setUserModel(null);
        setUserDocInfo(null);
    }

    public void loginOutSavePass()
    {
        if (null!=manService)
        {
            manService.getMANAnalytics().updateUserAccount("", "");
        }
        mPresenter.logout(userModel.getUserId());
        RongIM.getInstance().logout();
        setUserModel(null);
        setUserDocInfo(null);
    }

    public int getAppCount()
    {
        return 0;
    }


    public Group getGroupInfo(String groupId)
    {
        return mPresenter.getGroupInfoByGroupId(groupId);
    }

    public UserInfo getUserInfo(String userId)
    {
        return mPresenter.getUserInfoByUserId(userId);
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public MANService getManService() {
        return manService;
    }
}
